import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {SharedModule} from '../shared.module';
import {MODAL_WIDTH} from '../../const'
import areas from '../../const/area.json'
import { ApplicationRecord } from '../../types/resp/ApplicationRecord';
import { CandidateService } from '../../service/candidate.service';
import { EducationService } from '../../service/education.service';
import { EducationQueryForm } from '../../types/req/EducationQueryForm';
import { Education } from '../../types/resp/Education';
import { Page } from '../../types/page';
import { WorkExperience } from '../../types/resp/WorkExperience';
import { SkillCertificate } from '../../types/resp/SkillCertificate';
import { TrainingRecord } from '../../types/resp/TrainingRecord';
import { WorkExperienceService } from '../../service/work-experience.service';
import { SkillCertificateService } from '../../service/skill-certificate.service';
import { TrainingRecordService } from '../../service/training-record.service';
import { WorkExperienceQueryForm } from '../../types/req/WorkExperienceQueryForm';
import { SkillCertificateQueryForm } from '../../types/req/SkillCertificateQueryForm';
import { TrainingRecordQueryForm } from '../../types/req/TrainingRecordQueryForm';

@Component({
  selector: 'candidate-view-all',
  imports: [
    SharedModule
  ],
  templateUrl: './candidate-view-all.component.html',
  standalone: true,
  styleUrls: ['./candidate-view-all.component.less']
})
export class CandidateViewAllComponent implements OnInit {
  applicationRecordForm!: FormGroup;
  candidateForm!: FormGroup;
  isVisible: boolean = false
  title: string = '应聘者基本信息详情'
  modalWidth: string = MODAL_WIDTH
  provinceCityAreaOptions = areas.provinces;
  listOfEducation: Education[] = [];
  totalOfEducation: number = 0;
  listOfWorkExperience: WorkExperience[] = [];
  totalOfWorkExperience: number = 0;
  listOfSkillCertificate: SkillCertificate[] = [];
  totalOfSkillCertificate: number = 0;
  listOfTrainingRecord: TrainingRecord[] = [];
  totalOfTrainingRecord: number = 0;

  constructor(
    private fb: FormBuilder,
    private candidateService: CandidateService,
    private educationService: EducationService,
    private workExperienceService: WorkExperienceService,
    private skillCertificateService: SkillCertificateService,
    private trainingRecordService: TrainingRecordService
  ) {}

  ngOnInit(): void {
    this.applicationRecordForm = this.fb.group({
      enterpriseName: [''],
      jobDescriptionName: ['']
    })
    
    this.candidateForm = this.fb.group({
      username: [''],
      name: [''],
      password: [''],
      gender: [undefined],
      birthDate: [''],
      contactPhone: [''],
      email: [''],
      province: [''],
      city: [''],
      area: [''],
      provinceCityArea: [[]],
      address: ['']
    })
  }

  onBack() {
    this.isVisible = false
  }

  display(applicationRecord: ApplicationRecord) {
    this.isVisible = true
    this.applicationRecordForm.patchValue(applicationRecord);
    this.applicationRecordForm.disable();

    this.dislayCandidate(applicationRecord.candidateId)

    this.displayEducation(applicationRecord.candidateId)

    this.displayWorkExperience(applicationRecord.candidateId)

    this.displaySkillCertificate(applicationRecord.candidateId)

    this.displayTrainingRecord(applicationRecord.candidateId)
  }

  dislayCandidate(candidateId: number) {
    this.candidateService.findById(candidateId).subscribe({
      next: (resp) => {
        if (resp && resp.code === 1 && resp.data) {
          const provinceCityArea = []
          if (resp.data.province) {
            provinceCityArea.push(resp.data.province)
          }
          if (resp.data.city) {
            provinceCityArea.push(resp.data.city)
          }
          if (resp.data.area) {
            provinceCityArea.push(resp.data.area)
          }
          this.candidateForm.patchValue(Object.assign({provinceCityArea: provinceCityArea}, resp.data));
          this.candidateForm.disable();
        }
      },
      complete: () => {
      }
    })
  }

  displayEducation(candidateId: number) {
    this.educationService.find(new EducationQueryForm({candidateId: candidateId})).subscribe({
      next: (resp) => {
        if (!resp || resp.code !== 1) {
          return
        }
        const page: Page<Education> | undefined = resp.data
        if (page && page.list.length > 0) {
          this.listOfEducation = page.list
          this.totalOfEducation = page.total
        }
      }
    })
  }

  displayWorkExperience(candidateId: number) {
    this.workExperienceService.find(new WorkExperienceQueryForm({candidateId: candidateId})).subscribe({
      next: (resp) => {
        if (!resp || resp.code !== 1) {
          return
        }
        const page: Page<WorkExperience> | undefined = resp.data
        if (page && page.list.length > 0) {
          this.listOfWorkExperience = page.list
          this.totalOfWorkExperience = page.total
        }
      }
    })
  }

  displaySkillCertificate(candidateId: number) {
    this.skillCertificateService.find(new SkillCertificateQueryForm({candidateId: candidateId})).subscribe({
      next: (resp) => {
        if (!resp || resp.code !== 1) {
          return
        }
        const page: Page<SkillCertificate> | undefined = resp.data
        if (page && page.list.length > 0) {
          this.listOfSkillCertificate = page.list
          this.totalOfSkillCertificate = page.total
        }
      }
    })
  }

  displayTrainingRecord(candidateId: number) {
    this.trainingRecordService.find(new TrainingRecordQueryForm({candidateId: candidateId})).subscribe({
      next: (resp) => {
        if (!resp || resp.code !== 1) {
          return
        }
        const page: Page<TrainingRecord> | undefined = resp.data
        if (page && page.list.length > 0) {
          this.listOfTrainingRecord = page.list
          this.totalOfTrainingRecord = page.total
        }
      }
    })
  }
}
